/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package 解析.node.node;

import 解析.node.others.TreeInfo;
import 解析.Matcher;

/**
 *
 * @author yanxi
 */
public class UnixDollar extends Node { 
    public boolean multiline;

    public UnixDollar(boolean mul) {
        multiline = mul; 
    }
    
    @Override
    public boolean match(Matcher matcher, int i, CharSequence seq) {
        int endIndex = (matcher.anchoringBounds)
                ? matcher.to : matcher.getTextLength();
        if (i < endIndex) {
            char ch = seq.charAt(i);
            if (ch == '\n') {
                // If not multiline, then only possible to
                // match at very end or one before end
                if (multiline == false && i != endIndex - 1) {
                    return false;
                }
                // If multiline return next.match without setting
                // matcher.hitEnd
                if (multiline) {
                    return next.match(matcher, i, seq);
                }
            } else {
                return false;
            }
        }
        // Matching because at the end or 1 before the end;
        // more input could change this so set hitEnd
        matcher.hitEnd = true;
        // If a $ matches because of end of input, then more input
        // could cause it to fail!
        matcher.requireEnd = true;
        return next.match(matcher, i, seq);
    }

    @Override
    public boolean study(TreeInfo info) {
        next.study(info);
        return info.deterministic;
    }
}
